/*
* Portuguese eID Card API .NET Wrapper
*
* Written by Fernando Nunes (fernando.nunes@devscope.net)
* Copyright (c) 2009 DevScope Information Systems
*
* This library is free software; you can redistribute it and/or 
* modify it under the terms of the GNU Lesser General Public 
* License as published by the Free Software Foundation; either 
* version 2.1 of the License, or (at your option) any later version. 
* 
* This library is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
* Lesser General Public License for more details. 
* 
* You should have received a copy of the GNU Lesser General Public 
* License along with this library; if not, write to the Free Software 
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 
*/ 

#pragma once

#ifndef _WIN32_WINNT		// Allow use of features specific to Windows XP or later.                   
#define _WIN32_WINNT 0x0501	// Change this to the appropriate value to target other versions of Windows.
#endif						

#include <stdio.h>
#include <tchar.h>


#include <string>

// System::String -> std::string
std::string     StringManagedToUnmanaged(System::String^ managed);

// System::string -> char[]
void            StringManagedToUnmanaged(char* out, System::String^ in, unsigned int maxSize);

// const char* -> System::String
System::String^ StringUnmanagedToManaged(const char* unmanaged);

// System::Array<TypeSource> -> TypeDestination*
// The new length is returned. maxSize is to prevent buffer overflow, if the managed array size is smaller, only that part is copied
template<typename TypeSource, typename TypeDestination>
unsigned int ArrayManagedToUnmanaged(array<TypeSource>^ managed, TypeDestination* unmanaged, unsigned int maxSize)
{
    unsigned int i = 0;
    for(; maxSize > i && static_cast<unsigned int>(managed->Length) > i; ++i)
        unmanaged[i] = managed[i];
    return i;
}

// TypeSource* -> System::Array<TypeDestination>
template<typename TypeDestination, typename TypeSource>
array<TypeDestination>^ ArrayUnmanagedToManaged(TypeSource* unmanaged, unsigned int size)
{
    array<TypeDestination>^ managed = gcnew array<TypeDestination>(size);
    for(unsigned int i = 0; size > i; ++i)
        managed[i] = unmanaged[i];
    return managed;
}
